#include "precompiled.h"
#include "httpget.h"
using namespace weo::engine::utility;
using namespace weo::engine::character;
using namespace weo::engine::item;
using boost::asio::ip::tcp;
using namespace std;

// original source: Copyright (c) 2003-2007 Christopher M. Kohlhoff (chris at kohlhoff dot com)
HttpGet::HttpGet(std::string host, std::string file, std::string content)
{
    try
    {
        m_Error = false;
        std::ostringstream oss;
        boost::asio::io_service io_service;

        // Get a list of endpoints corresponding to the server name.
        tcp::resolver resolver(io_service);
        tcp::resolver::query query(host, "http");
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        tcp::resolver::iterator end;

        // Try each endpoint until we successfully establish a connection.
        tcp::socket socket(io_service);
        boost::system::error_code error = boost::asio::error::host_not_found;
        while (error && endpoint_iterator != end)
        {
            socket.close();
            socket.connect(*endpoint_iterator++, error);
        }
        if (error)
            throw boost::system::system_error(error);

        boost::asio::streambuf request;
        std::ostream request_stream(&request);
        request_stream << "GET " << file << " HTTP/1.0\r\n";
        request_stream << "Host: " << host << "\r\n";
        request_stream << "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n";
        request_stream << "Accept: " << content << ";charset=utf-8\r\n";
        request_stream << "Connection: close\r\n\r\n";

        // Send the request.
        boost::asio::write(socket, request);
        // Read the response status line.
        boost::asio::streambuf response;
        boost::asio::read_until(socket, response, "\r\n");

        // Check that response is OK.
        std::istream response_stream(&response);
        std::string http_version;
        response_stream >> http_version;
        response_stream >> m_StatusCode;
        std::string status_message;
        std::getline(response_stream, status_message);
        if (!response_stream || http_version.substr(0, 5) != "HTTP/")
        {
            m_Error = true;
            return;
        }
        if (m_StatusCode != 200)
        {		
            m_Error = true;
            return;
        }

        // Read the response headers, which are terminated by a blank line.
        boost::asio::read_until(socket, response, "\r\n\r\n");
        // Read until EOF, writing data to output as we go.
        while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error))
        {
            oss << &response;
        }
        if (error != boost::asio::error::eof)
        {
            throw boost::system::system_error(error);
        }
        m_Data = oss.str();
        std::string::size_type pos = m_Data.find("<?xml version");
        if(pos != std::string::npos)
        {
            m_Data.replace(0,pos,"");
        }
    }
    catch (std::exception& e)
    {
    }
}